#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2019-12-05 09:54:01
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：

import stomp
import time_ext_
import logging
log=logging.getLogger('main.AMQ')

#消费者数据处理
class SampleListener(object):
    def on_message(self, headers, msg):
        print(headers)
        log.warning('接收来自队列：{},消息-->:\n**********\n{}\n**********\n'.format(msg))
        print(1111,headers,msg)


class AMQ(object):
    """docstring for AMQ"""
    connect=None
    def __init__(self, host='127.0.0.1',port=61613):
        super(AMQ, self).__init__()
        self.host=host
        self.port=port
        self.log=log
        self.connect=self.Init_Connect()

    #初始AMQ连接
    def Init_Connect(self):
        if not self.connect:
            self.log.warning("连接到AMQ服务,{}:{} 成功".format(self.host,self.port))
            return stomp.Connection10([(self.host,self.port)])
        else:
            return self.connect
    #发送消息到AMQ
    def Send_Message(self,name,msg):
        try:
            self.connect.start()
            self.connect.connect()
            self.Init_Connect().send(name,msg)
            self.log.warning("发送消息,队列：{}，消息-->:\n**********\n{}\n**********\n".format(name,msg))
            return True
        except Exception as e:
            self.log.warning("发送数据失败,{}".format(e))
            return False
    def Receive_Message(self,AmqId,work=SampleListener):
        try:
            self.connect.start()
            self.connect.connect()
            self.connect.set_listener('',work())
            self.connect.subscribe(AmqId)
            return True
        except Exception as e:
            return None
    def colse(self):
        self.connect.disconnect()
if __name__ == '__main__':
    app=AMQ()
    # app.Receive_Message("icstocics")
    # app.connect.receive_frame(SampleListener())
    # print(app.connect)
    # print(dir(app.connect))

    while 1:
        # aa=app.Send_Message(   'icstocics','测试icsTO2343')
        # print(aa)
        time_ext_.sleep(3)
        app.Receive_Message('cicstoics')

        # print()